
#ifndef TOKZRGENTR_GEN_GEN_H
#define TOKZRGENTR_GEN_GEN_H

#include"ivisitors.h"
#include"tokenizer_wrapper.h"

namespace TokzrGentr
{
	class code_gen : public syntax_node_ivisitor
	{
	private:
		std::shared_ptr<tokenizer_wrapper> _wrapper;
		std::shared_ptr<CppOutputTemplate> _cpp_output;
	public:
		code_gen() {
			_wrapper = std::make_shared<tokenizer_wrapper>();
			_cpp_output = std::make_shared<CppOutputTemplate>();
		}
		void visit(stmt *node) override
		{
			auto tokname = node->decl()->identifier()->Name();
			auto rank = node->rank();
			auto re = node->decl()->regex()->regex_str();
			_wrapper->AddRule(tokname, re, rank);
		}

		void visit(stmts *node) override
		{
			for (auto s : node->all_stmts())
				s->accept(this);
		}

		void visit(regex *node) override{}

		void visit(decl *node) override{}

		void visit(token_identifier *node) override{}

		void visit(root *node) override
		{
			auto tokzrname = node->TokzrName();
			_cpp_output->TokzrClassName = tokzrname;
			node->stmts()->accept(this);
		}

		std::u16string CodeGen(syntax_node *node) {
			node->accept(this);
			return _wrapper->BuildAsOutputFile(_cpp_output.get());
		}
	};
}
#endif // !TOKZRGENTR_GEN_GEN_H
